package com.bases.ScollBar
{
	import com.bases.ImageButton;
	import com.Timer.FrameTimerEvent;
	import com.debug.TDebug;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	/**
	 * 普通控件的ScrollBar(针对电子白板的,其它的要重写)
	 * @author pethan
	 * 
	 */	
	public class TwoScrollBar extends TBaseScrollBar
	{
		private var controlTarget:Sprite;
		public var maskHeight:Number;
		private var thisWidth:Number;
		private var yMax:Number = 0;
		private var speed:Number = 10;
		private var yMin:Number = 0;
		
		private var targetHeight:Number=0;
		
		public function TwoScrollBar(_target:Sprite)
		{
			super();
			this.controlTarget=_target;
			this.yMin=TBaseScrollBar.BUTTON_HEIGHT;
		}
		
//		public function setPosH(_x:Number,_y:Number):void{
//			var point:Point=new Point(_x,_y);
//			var newPoint:Point=this.globalToLocal(point);
//			_x=newPoint.x;
//			_y=newPoint.y;
//			this.btnUp.setPos(_x,_y);
//			this.btnTrack.setPos(_x,_y+TBaseScrollBar.BUTTON_HEIGHT);
//			this.btnScroll.setPos(_x,_y+TBaseScrollBar.BUTTON_HEIGHT);
//			this.btnDown.setPos(_x,this.btnScroll.y+this.btnScroll.height);
//		}
//		
//		public function setPosV(_x:Number,_y:Number):void{
//			this.btnLeft.setPos(_x,_y);
//			this.btnVerTrack.setPos(_x+	TBaseScrollBar.BUTTON_WIDTH,_y);
//			this.btnVerScroll.setPos(_x+TBaseScrollBar.BUTTON_WIDTH,_y);
//			this.btnRight.setPos(this.btnVerTrack.x+this.btnTrack.width,_y);
//		}
		
//		public function invalidateScrollBar():void{
//			targetHeight=int(controlTarget.height);
//			var isHScroll:Boolean = targetHeight > maskHeight;
//			if (isHScroll)
//			{
//				this.thisWidth = (this.btnTrack.height - TBaseScrollBar.BUTTON_HEIGHT * 2) * (1 - (targetHeight - maskHeight) / targetHeight);
//				this.btnScroll.setSize(this.btnScroll.width,this.thisWidth);
//				this.btnScroll.y = this.btnUp.y + TBaseScrollBar.BUTTON_HEIGHT;
//			}
//			this.yMax=this.btnDown.y-this.btnScroll.height;
//			thumbScrollV();//改变位置
//			if (isHScroll)
//			{
//				this.hVisible=true;
//			}
//			else
//			{
//				this.hVisible=false;
//			}
//		}
		
		private function set hVisible(value:Boolean):void{
			this.btnUp.visible=value;
			this.btnDown.visible=value;
			this.btnScroll.visible=value;
			this.btnTrack.visible=value;
		}
		
		private function set vVisible(value:Boolean):void{
			this.btnLeft.visible=value;
			this.btnRight.visible=value;
			this.btnVerScroll.visible=value;
			this.btnVerTrack.visible=value;
		}
		
		public function invalidateScrollBarHeight(_height:Number,_width:Number):void{
			var isHScroll:Boolean =_height > maskHeight;
			var isVScroll:Boolean=_width>maskWidth;
			targetHeight=_height;
			targetWidth=_width;
			if (isHScroll)
			{
				this.btnTrack.setSize(this.btnTrack.width,maskHeight-2*TBaseScrollBar.BUTTON_HEIGHT);
				this.thisWidth = (this.btnTrack.height) * (1 - (_height - maskHeight) / _height);
				this.btnScroll.setSize(this.btnScroll.width,this.thisWidth);
				this.btnScroll.y = this.btnUp.y + TBaseScrollBar.BUTTON_HEIGHT;
				this.btnDown.y=this.btnScroll.y+this.btnTrack.height;
				
				this.yMax=this.btnDown.y-this.btnScroll.height;
				this.hVisible=true;
				thumbScrollV();//改变位置
			}else{
				this.controlTarget.y=0;
				this.hVisible=false;
			}
			
			
			if (isVScroll)
			{
				this.btnVerTrack.setSize(this.btnTrack.width,maskWidth-2*TBaseScrollBar.BUTTON_WIDTH);
				var scrollWidth:Number=(this.btnVerTrack.width)*(1-(_width-maskWidth)/_width);
				this.btnVerScroll.setSize(scrollWidth,this.btnVerScroll.width);
				this.btnVerScroll.y=this.btnLeft.y+TBaseScrollBar.BUTTON_WIDTH;
				this.btnRight.y=this.btnVerScroll.y+this.btnVerScroll.width;
				
				this.xMax=this.btnRight.y-this.btnVerScroll.width;
				this.vVisible=true;
				thumbScrollH();//改变位置
			}
			else
			{
				this.controlTarget.x=0;
				this.vVisible=false;
			}
		}
		
		override protected function onBackBGDown(event:MouseEvent):void{
			super.onBackBGDown(event);
			thumbScrollV();
		}
		
		
		override protected function onUp(event:MouseEvent):void{
			this.btnScroll.y -= this.speed;
			if (btnScroll.y <= yMin)
			{
				btnScroll.y = yMin;
			}
			super.onUp(event);
		}
		
		override protected function onDown(event:MouseEvent):void{
			this.btnScroll.y += this.speed;
			if (btnScroll.y >= yMax)
			{
				btnScroll.y = yMax;
			}
			super.onDown(event);
		}
		
		
		override protected function onTimer(event:FrameTimerEvent):void{
			if(type=='down'){
				btnScroll.y+= this.speed;
				if(this.btnScroll.y>=yMax){
					btnScroll.y=yMax;
				}
			}else{
				btnScroll.y-= this.speed;
				if(this.btnScroll.y<=this.yMin){
					this.btnScroll.y=this.yMin;
				}
			}
			super.onTimer(event);
		}
		
		override protected function thumbScrollV():void{
			var scrollPercent:Number=(btnScroll.y - TBaseScrollBar.BUTTON_HEIGHT) / (yMax - TBaseScrollBar.BUTTON_HEIGHT);
			this.controlTarget.y = - scrollPercent * (targetHeight - maskHeight);
		}
		
		protected function thumbScrollH():void{
			var scrollPercent:Number=(btnVerScroll.x - TBaseScrollBar.BUTTON_WIDTH) / (xMax - TBaseScrollBar.BUTTON_WIDTH);
			this.controlTarget.x= - scrollPercent * (targetWidth - maskWidth);
		}
		
		override protected function onStateMove(event:MouseEvent):void{
			thumbScrollV();
		}
		
		protected function onStateVerMove(event:MouseEvent):void{
			this.thumbScrollH();
		}
		
	}
}